//aes加密
function encrypt_str(str,mykey,myiv) {
	var key = CryptoJS.enc.Utf8.parse(mykey);
	var iv = CryptoJS.enc.Utf8.parse(myiv);
	var encrypted = '';
	var srcs = CryptoJS.enc.Utf8.parse(str);
	encrypted = CryptoJS.AES.encrypt(srcs,key,{
		iv:iv,
		mode:CryptoJS.mode.CBC,
		padding:CryptoJS.pad.Pkcs7,
	})
	return encrypted.ciphertext.toString();
}

//aes解密
function decrypt_str(str,mykey,myiv){
	var key = CryptoJS.enc.Utf8.parse(mykey);
	var iv = CryptoJS.enc.Utf8.parse(myiv);
	var encryptedHexStr = CryptoJS.enc.Hex.parse(str);
	var srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
	var decrypt = CryptoJS.AES.decrypt(srcs,key,{
		iv:iv,
		mode:CryptoJS.mode.CBC,
		padding:CryptoJS.pad.Pkcs7
	})
	return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}

//固定的解密
function decrypt_str_fix(str){
	var key = CryptoJS.enc.Utf8.parse("NLfZ3aIOg77E2rT8");
	var iv = CryptoJS.enc.Utf8.parse("0102030405060708");
	var encryptedHexStr = CryptoJS.enc.Hex.parse(str);
	var srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
	var decrypt = CryptoJS.AES.decrypt(srcs,key,{
		iv:iv,
		mode:CryptoJS.mode.CBC,
		padding:CryptoJS.pad.Pkcs7
	})
	return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}

//判断是否为空或者是否存在
function is_null(str){
	if (typeof str === 'undefined' || str === null || str === '') return true;
	else return false;
}

//按纽禁用
function dis(str)
{
	document.getElementById(str).disabled="disabled";
}

//按纽恢复
function nodis(str)
{
	document.getElementById(str).disabled="";
}

//页面载入焦点
function init(str)
{
	var old_value=document.getElementById(str).value;
	document.getElementById(str).value="";
	document.getElementById(str).focus();
	document.getElementById(str).value=old_value;
}


//回车换行
function on_key_tab(obj,e)
{
	var key = window.event ? e.keyCode:e.which;
	if(key*1==13)
	{
		init(obj);
	}
}
//回车换行
function on_key_tab_s(id,e)
{
	var key = window.event ? e.keyCode:e.which;
	if(key*1==13)
	{
		let obj=$("#"+id);
		obj.parent().find(".layui-select-title").find("input").focus();
		obj.parent().find(".layui-select-title").find("input").click();
	}
}

//layui select获取焦点
function init_select(id)
{
	let obj=$("#"+id);
	obj.parent().find(".layui-select-title").find("input").focus();
	obj.parent().find(".layui-select-title").find("input").click();
}

//回车执行按钮
function on_key_button(obj,e)
{
	var key = window.event ? e.keyCode:e.which;
	if(key*1==13)
	{
		document.getElementById(obj).click();
	}
}

//回车执行 on_query
function on_key_query(e)
{
	var key = window.event ? e.keyCode:e.which;
	if(key*1==13)
	{
		on_query();
	}
}

function check_login(code){
	if (code==6) parent.parent.parent.window.location.href="../../../../../../wxht/login";
}

//ajax发送JSON，不带令牌
function post_json(url,data,msg,callback){
	let loadIndex = layer.msg(msg, {icon: 16, shade: 0.01, time: false});
	$.ajax({
		type:"post",
		url:url,
		data:data,
		dataType:"json",
		timeout : 30*1000, //超时时间设置，单位毫秒
		crossDomain: true,
		contentType: 'application/json;charset=UTF-8',
		beforeSend: function(request) {
			request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
		},
		success:function(result){
			layer.close(loadIndex);
			callback(result);
		},
		error:function(result){
			layer.close(loadIndex);
			callback(result);
		}
	});
}

//ajax发送JSON 带令牌
function post_json_token(url,data,msg){
	let loadIndex = layer.msg(msg, {icon: 16, shade: 0.01, time: false});
	//console.info("发送的data:"+data);
	$.ajax({
		type:"post",
		url:url,
		data:data,
		dataType:"json",
		timeout : 180*1000, //超时时间设置，单位毫秒
		crossDomain: true,
		contentType: 'application/json;charset=UTF-8',
		headers: {
			"token": localStorage.getItem("token"),
		},
		beforeSend: function(request) {
			request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
		},
		success:function(result){
			layer.close(loadIndex);
			if (result.code==6) parent.parent.parent.window.location.href="../../../../../../wxht/login";
			on_result(result);
		},
		error:function(result){
			layer.close(loadIndex);
			if (result.code==6) parent.parent.parent.window.location.href="../../../../../../wxht/login";
			on_result(result);
		}
	});
}

function post_json_token_back(url,data,msg,callback){
	let loadIndex = layer.msg(msg, {icon: 16, shade: 0.01, time: false});
	//console.info("发送的data:"+data);
	$.ajax({
		type:"post",
		url:url,
		data:data,
		dataType:"json",
		timeout : 180*1000, //超时时间设置，单位毫秒
		crossDomain: true,
		contentType: 'application/json;charset=UTF-8',
		headers: {
			"token": localStorage.getItem("token"),
		},
		beforeSend: function(request) {
			request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
		},
		success:function(result){
			layer.close(loadIndex);
			if (result.code==6) parent.parent.parent.window.location.href="../../../../../../wxht/login";
			callback(result);
		},
		error:function(result){
			layer.close(loadIndex);
			if (result.code==6) parent.parent.parent.window.location.href="../../../../../../wxht/login";
			callback(result);
		}
	});
}

function post_json_token_download(url,data,msg,url1){
	let loadIndex = layer.msg(msg, {icon: 16, shade: 0.01, time: false});
	//console.info("发送的data:"+data);
	$.ajax({
		type:"post",
		url:url,
		data:data,
		dataType:"json",
		timeout : 180*1000, //超时时间设置，单位毫秒
		crossDomain: true,
		contentType: 'application/json;charset=UTF-8',
		headers: {
			"token": localStorage.getItem("token"),
		},
		beforeSend: function(request) {
			request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
		},
		success:function(result){
			layer.close(loadIndex);
			if (result.code==7){
				layer.msg('文件已生成，准备下载', { time: 1000 }, function() {
					let fileId = result.data;
					//alert(fileId);
					var a = url1+"?fileId="+fileId+"&token="+localStorage.getItem("token")+"";
					window.open(a,'_blank');
				});
			}else{
				alert(result.msg);
			}
			if (result.code==6) parent.parent.parent.window.location.href="../../../../../../wxht/login";
		},
		error:function(result){
			layer.close(loadIndex);
			if (result.code==6) parent.parent.parent.window.location.href="../../../../../../wxht/login";
		}
	});
}
//带文件提交
function post_data_file_token(url,data,msg){
	let loadIndex = layer.msg(msg, {icon: 16, shade: 0.01, time: false});
	//console.info("发送的data:"+data);
	$.ajax({
		type:"post",
		url:url,
		data:data,
		dataType: 'json',
		timeout : 280*1000, //超时时间设置，单位毫秒
		crossDomain: true,
		processData: false,   // jQuery不要去处理发送的数据
		contentType: false,   // jQuery不要去设置Content-Type请求头
		headers: {
			"token": localStorage.getItem("token")
			// 'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
		},
		beforeSend: function(request) {
			request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
		},
		success:function(result){
			layer.close(loadIndex);
			if (result.code==6) {
				parent.parent.parent.window.location.href="../../../../../../wxht/login";
			}else {
				on_result(result);
			}
		},
		error:function(result){
			layer.close(loadIndex);
			if (result.code==6) parent.parent.parent.window.location.href="../../../../../../wxht/login";
			layer.msg("发生错误！", { time: 2000 }, function() {
			});
		}
	});
}
//表格数据查询
function post_json_table(url,data,msg,callback){
	let loadIndex = layer.msg(msg, {icon: 16, shade: 0.01, time: false});
	$.ajax({
		type:"post",
		url:url,
		data:data,
		dataType:"json",
		timeout : 280*1000, //超时时间设置，单位毫秒
		crossDomain: true,
		contentType: 'application/json;charset=UTF-8',
		headers: {
			"token": localStorage.getItem("token")
		},
		beforeSend: function(request) {
			request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
		},
		success:function(result){
			layer.close(loadIndex);
			if (result.code==0){
				let rtn = decrypt_str(result.data,localStorage.getItem("a"),localStorage.getItem("b"));
				callback(JSON.parse(rtn));
			}else if (result.code==6) {
				parent.parent.parent.window.location.href="../../../../../../wxht/login";
			}else if (result.code==500){
				layer.msg(result.msg, { time: 2000 }, function() {
				});
			}else {
				layer.msg(result.msg, { time: 2000 }, function() {
				});
			}
		},
		error:function(result){
			layer.close(loadIndex);
			if (result.code==6) parent.parent.parent.window.location.href="../../../../../../wxht/login";
		}
	});
}
//查询界面，参数获取
function post_json_form(url,data,msg,callback){
	$.ajax({
		type:"post",
		url:url,
		data:data,
		dataType:"json",
		timeout : 160*1000, //超时时间设置，单位毫秒
		crossDomain: true,
		contentType: 'application/json;charset=UTF-8',
		headers: {
			"token": localStorage.getItem("token")
		},
		beforeSend: function(request) {
			request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
		},
		success:function(result){
			if (result.code==0){
				let rtn = decrypt_str(result.data,localStorage.getItem("a"),localStorage.getItem("b"));
				callback(JSON.parse(rtn));
			}else if (result.code==6) {
				parent.parent.parent.window.location.href="../../../../../../wxht/login";
			}else if (result.code==500){
				layer.msg(result.msg, { time: 2000 }, function() {
				});
			}else {
				on_result(result);
			}
		},
		error:function(result){
			if (result.code==6) parent.parent.parent.window.location.href="../../../../../../wxht/login";
			on_result(result);
		}
	});
}
//获取提交令牌
function psot_get_nonce(ms,url,callback){
	var a = {
		ms: ms,
		timestamp:new Date().getTime()
	};
	var b = {
		data:encrypt_str(JSON.stringify(a),localStorage.getItem("a"),localStorage.getItem("b"))
	};
	$.ajax({
		type:"post",
		url:url,
		data:JSON.stringify(b),
		dataType:"json",
		timeout : 10*1000, //超时时间设置，单位毫秒
		crossDomain: true,
		contentType: 'application/json;charset=UTF-8',
		headers: {
			"token": localStorage.getItem("token") // 在请求头中添加token
		},
		beforeSend: function(request) {
			request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
		},
		success:function(result){
			if (result.code==6) parent.parent.parent.window.location.href="../../../../../../wxht/login";
			if (result.code==500) {
				on_result(result);
			}else{
				let rtn = decrypt_str(result.data,localStorage.getItem("a"),localStorage.getItem("b"));
				callback(rtn);
			}
		},
		error:function(result){
			if (result.code==6) parent.parent.parent.window.location.href="../../../../../../wxht/login";
			on_result(result);
		}
	});
}

function psot_get_nonce_h5(ms,url,callback){
	var a = {
		ms: ms,
		timestamp:new Date().getTime()
	};
	var b = {
		data:encrypt_str(JSON.stringify(a),localStorage.getItem("a_h5"),localStorage.getItem("b_h5"))
	};
	$.ajax({
		type:"post",
		url:url,
		data:JSON.stringify(b),
		dataType:"json",
		timeout : 10*1000, //超时时间设置，单位毫秒
		crossDomain: true,
		contentType: 'application/json;charset=UTF-8',
		headers: {
			"token": localStorage.getItem("token_h5") // 在请求头中添加token
		},
		beforeSend: function(request) {
			request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
		},
		success:function(result){
			if (result.code==6) {
				var dz = window.location.href;
				dz = dz.replace(/&/g, "$");
				if (result.code==6) window.location.href = adminUrl+"/h5/login?backurl=" + encodeURIComponent(dz);
			};
			if (result.code==500) {
				on_result(result);
			}else{
				let rtn = decrypt_str(result.data,localStorage.getItem("a_h5"),localStorage.getItem("b_h5"));
				callback(rtn);
			}
		},
		error:function(result){
			on_result(result);
		}
	});
}